package cn.ucox.web.ms.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;


/**
 * Redis数据序列化
 *
 * @author chenw
 * @email javacspring@gmail.com
 * @time 2016/11/10
 */
public class RedisJsonSerializer implements RedisSerializer<Object> {

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if (null == o)
            return new byte[0];
        return JSON.toJSONBytes(o, SerializerFeature.WriteClassName);
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (null != bytes && bytes.length > 0)
            return JSON.parse(bytes);
        return null;
    }
}
